<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索 -->
      <el-form v-show="showSearch" :model="queryForm" :inline="true" label-width="68px">
        <el-form-item label="" prop="name">
          <el-input v-model="queryForm.name" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search" />
        </el-form-item>
        <el-form-item>
          <el-button plain type="primary" icon="el-icon-search" size="mini" @click="searchQuery">搜索</el-button>
          <el-button plain type="warning" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 新增，编辑，删除，导出 -->
      <el-button
        v-hasPermi="['admin','system:${lowerClassName}:add']" type="primary"
        small
        icon="el-icon-plus"
        size="mini"
        @click="addDialogVisible = true"
      >新增
      </el-button>
      <el-button
        v-hasPermi="['admin','system:${lowerClassName}:edit']"
        :disabled="ids.length===1?false:true"
        type="success"
        small
        icon="el-icon-edit"
        style="margin:10px"
        size="mini"
        @click="editBtn(ids[0])"
      >修改
      </el-button>
      <el-button
        v-hasPermi="['admin','system:${lowerClassName}:del']"
        :disabled="ids.length>0?false:true"
        type="danger"
        small
        icon="el-icon-delete"
        style="margin:10px"
        size="mini"
        @click="delAllBtn()"
      >删除
      </el-button>
      <el-button
        v-hasPermi="['admin','system:${lowerClassName}:export']"
        type="warning"
        small
        icon="el-icon-download"
        style="margin:10px"
        size="mini"
        @click="exportClick"
      >导出
      </el-button>
      <!-- 搜索，刷新，表头工具栏 -->
      <right-bar :show-search.sync="showSearch" :table-data="tableColumns" @queryTable="refresh()" />

      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <#list  fields as field>
        <el-table-column v-if="hasTable('${field.fieldName}',tableColumns)" prop="${field.fieldName}" label="${field.description}" />
        </#list>
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-hasPermi="['admin','system:${lowerClassName}:edit']" type="text" size="mini" @click="editBtn(scope.row.<#list fields as field><#if field.primaryKey>${field.fieldName}</#if></#list>)">编辑</el-button>
            <el-button v-hasPermi="['admin','system:${lowerClassName}:del']" type="text" size="mini" @click="delBtn(scope.row.<#list fields as field><#if field.primaryKey>${field.fieldName}</#if></#list>)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="600px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <#list  fields as field>
            <#if !field.primaryKey>
          <el-form-item label="${field.description}" prop="${field.fieldName}">
            <el-input v-model="addForm.${field.fieldName}" />
          </el-form-item>
            </#if>
          </#list>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addDialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="600px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
         <#list  fields as field>
             <#if !field.primaryKey>
          <el-form-item label="${field.description}" prop="${field.fieldName}">
            <el-input v-model="editForm.${field.fieldName}" />
          </el-form-item>
             </#if>
         </#list>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editDialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, get${className}, list${className}, export${className} } from '@/api/${module}/${lowerClassName}'
import { downloadFile } from '@/utils/index'

export default {
  name: '${className}',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      showSearch: true,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      ids: [],
      listForm: [],
      addForm: {
        <#list  fields as field>
        ${field.fieldName}: '',
        </#list>
      },
      queryForm: {
        name: ''
      },
      editForm: {},
      tableColumns: [
        <#list  fields as field>
        {
          label: '${field.description}',
          value: '${field.fieldName}',
          visible: true
          },
          </#list>
      ],
      addFormRules: {
        <#list  fields as field>
        ${field.fieldName}: [
          { required: true, message: '请输入${field.description}', trigger: 'blur' }
        ],
        </#list>
      }
    }
  },

  created() {
    // 初始化列表
    this.initList${className}()
  },

  methods: {
    // 查询按钮
    searchQuery() {
      this.searchList${className}()
    },

    // 重置按钮
    resetQuery() {
      this.queryForm = {}
      this.initList${className}()
    },

    // 刷新按钮
    refresh() {
      this.searchList${className}()
    },

    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchList${className}()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      get${className}(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchList${className}()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 批量删除按钮
    delAllBtn() {
      const data = {
        ids: this.ids
      }
      this.del${className}(data)
    },

    // 删除按钮
    delBtn(id) {
      this.ids = []
      this.ids.push(id)
      const data = {
        ids: this.ids
      }
      this.del${className}(data)
      // 清空ids数组
      this.ids = []
    },

    // 删除方法
    del${className}(id) {
      this.$confirm('此操作将永久删除【' + this.ids.length + '条数据】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.searchList${className}()
          }
        })
      })
    },

    // 导出
    exportClick() {
      const tableName = '${tableName}'
      this.$confirm('是否确认导出所有用户数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
      export${className}().then(data => {
          downloadFile(data, tableName, 'xlsx')
        })
      })
    },

    // 表格全选
    handleSelectionChange(val) {
      this.ids = []
      for (let index = 0; index < val.length; index++) {
        this.ids.push(val[index].<#list fields as field><#if field.primaryKey>${field.fieldName}</#if></#list>)
      }
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.searchList${className}()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.searchList${className}()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 条件查询
    searchList${className}() {
      const page = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      list${className}(this.queryForm, page).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },

    // 初始化数据
    initList${className}() {
      const page = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      list${className}(null, page).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
